<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Good_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $good_id
     * @return mixed
     * 查询单条
     */
    public function get_shop_goods_by_id($good_id){

        $this->db->select('*');
        $this->db->from('shop_goods');
        $this->db->where('good_id',$good_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * @param $good_id
     * @param $data
     * @return bool
     * 修改商品信息
     */
    public function sava_shop_goods($good_id,$data){

        $this->db->where('good_id',$good_id);
        $this->db->update('shop_goods', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增商品
     */
    public function add_shop_goods($data){

        $this->db->insert('shop_goods',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有商品
     */
    public function get_all_shop_goods($start,$limit,$uid=""){
        $this->db->select('*');
        $this->db->from('shop_goods');
        if(!empty($uid)){
            $this->db->where('user_id',$uid);
        }
        $this->db->limit($limit,$start);
        $this->db->order_by('good_id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }


    /**
     * 获取启用的规则
     */
    public function get_shop_goods_by($map = "status=1"){
        $sql = "SELECT * FROM {$this->db->dbprefix}shop_goods  where $map ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除商品
     */
    public function delete_shop_goods_by($where){
        $this->db->where($where)->delete('shop_goods');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }


    public function count_goods_list_by_catagory_id($category_tid){
        $this->db->select('good_id');
        $this->db->from('shop_goods');
        $this->db->where('good_category',$category_tid);
        $this->db->where('good_stock >',0);
        $this->db->where('good_lock',0);
        $total=$this->db->count_all_results();
        return $total;
    }

    /*喜欢频道*/
    public function get_goods_list_by_catagory_id($page=0,$limit=4,$category_tid){
        $start = $page*$limit;
        $query = $this->db->select('*');
        $this->db->from('shop_goods');
        $this->db->where('good_category',$category_tid);
        $this->db->where('good_stock >',0);
        $this->db->where('good_lock',0);
        $this->db->limit($limit,$start);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function is_favorites($user_id,$category_tid){
        $query = $this->db->select('*');
        $this->db->from('user_categorys');
        $this->db->where('user_id',$user_id);
        $this->db->where('category_tid',$category_tid);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }        
    }
    public function add_user_categorys($data){
        return $this->db->insert('user_categorys',$data);

    }
    public function updates_user_categorys_catogory_view($id){
        $sql = "update {$this->db->dbprefix}user_categorys set catogory_view=catogory_view +1 where category_id = ".$id;
        $query=$this->db->query($sql);
    }
    //根据搜索用户名的关键字查找商品
    public function get_goods_by_keyword($keywords,$order,$start,$limit){
        $query = $this->db->select('*');
        $this->db->from('shop_goods');
        $this->db->like('good_title', $keywords, 'both');
        $this->db->limit($limit,$start);
        $this->db->order_by('good_add_time',$order);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //计算搜索的关键字
    public function get_total_goods_by_keyword($keywords){
        $this->db->select('good_id');
        $this->db->from('shop_goods');
        $this->db->like('good_title', $keywords, 'both');
        $total=$this->db->count_all_results();
        return $total;
    }
    //查找所有参与免单的商品
    public function get_all_vouchers_goods($start,$limit){
        $this->db->select('*')->from('shop_goods')->where('good_voucher',1)->limit($limit,$start);
        $this->db->order_by('good_id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //计算所有的参与免单的商品
    public function count_all_vouchers_good(){
        $this->db->select('good_id');
        $this->db->from('shop_goods');
        $this->db->where('good_voucher',1);
        $total=$this->db->count_all_results();
        return $total;
    }
    //删除商品
    public function delete_shop_goods_by_id($id){
        $this->db->where('good_id',$id)->delete('shop_goods');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function get_goods_list_by_cate_id($category_id,$user_id){

        $this->db->select('shop_goods.good_id,shop_goods.good_title,shop_goods.good_memo');
        $this->db->from('shop_goods');
        $this->db->where('good_category',$category_id);
        $this->db->where('user_id',$user_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function get_good_property_by_id($good_id){
        $this->db->select('*');
        $this->db->from('shop_goods');
        $this->db->where('good_id',$good_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function get_goods_by_uid($uid){
        $this->db->select('category.category_name,shop_goods.*');
        $this->db->from('shop_goods');
        $this->db->join('category','shop_goods.good_category = category.id','left');
        $this->db->where('shop_goods.user_id',$uid);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    public function get_shop_goods_by_ugid($uid,$good_id){

        $this->db->select('*');
        $this->db->from('shop_goods');
        $this->db->where('good_id',$good_id);
        $this->db->where('user_id',$uid);
        $query = $this->db->get();
        return $query->row_array();
    }


    public function count_by_user_id($user_id){

        $this->db->select('good_id');
        $this->db->from('shop_goods');
        $this->db->where('user_id',$user_id);
        $total=$this->db->count_all_results();
        return $total;
    }
}
